package com.example.touchscaleview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * @ProjectName: TouchScaleView
 * @Package: com.example.touchscaleview
 * @ClassName: ScaleTextView
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2021/8/11 11:36
 * @UpdateUser: 更新者：
 * @UpdateDate: 2021/8/11 11:36
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ScaleTextView extends TextView {

    private float mTextSize;
    private float mFirstTouchX;
    private float mFirstTouchY;
    private float mSecondTouchX;
    private float mSecondTouchY;
    private double firstPointerLength;

    public ScaleTextView(Context context) {
        this(context,null);
    }

    public ScaleTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScaleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextSize = getTextSize();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                //获取第一个点（A点）的位置
                mFirstTouchX = event.getX();
                mFirstTouchY = event.getY();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                if(event.getActionIndex() == 1){
                    //获取第二个点（B点）的位置
                    mSecondTouchX = event.getX(1);
                    mSecondTouchY = event.getY(1);
                    //根据两点的位置获取两个触摸点之间的距离（AB）
                    float firstLengthX = Math.abs(mFirstTouchX - mSecondTouchX);
                    float firstLengthY = Math.abs(mFirstTouchY - mSecondTouchY);
                    firstPointerLength = Math.sqrt(Math.pow(firstLengthX, 2) + Math.pow(firstLengthY, 2));
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if(event.getPointerCount() >= 2){
                    //获取第一个点（A‘）的位置
                    float firstX = event.getX(0);
                    float firstY = event.getY(0);

                    //获取第二个点（B‘）的位置
                    float secondX = event.getX(1);
                    float secondY = event.getY(1);

                    //计算两点之间的距离（A'B'）
                    float secondLengthX = Math.abs(firstX - secondX);
                    float secondLengthY = Math.abs(firstY - secondY);
                    double secondPointerLength = Math.sqrt(Math.pow(secondLengthX, 2) + Math.pow(secondLengthY, 2));
                    //缩放文字
                    zoom(secondPointerLength);
                }
                break;
            case MotionEvent.ACTION_UP:
                //手指抬起的时候记录当前的文字大小
                mTextSize = getTextSize();
                break;
        }
        return true;
    }

    private void zoom(double secondPointerLength) {
        double scaleRate = secondPointerLength / firstPointerLength;
        float textSize = (float) (mTextSize * scaleRate);
        //这里也进行像素值赋值
        setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
    }
}
